package com.jr.controller;

import com.google.gson.Gson;
import com.jr.entity.Enterprise;
import com.jr.entity.Users;
import com.jr.pojo.SocialCode;
import com.jr.pojo.UserEnterprise;
import com.jr.service.EnterpriseService;
import com.jr.service.UserService;
import com.jr.serviceimpl.EnterpriseServiceImpl;
import com.jr.serviceimpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;


@WebServlet("/user/*")
public class UserController extends HttpServlet {
    UserService userService = new UserServiceImpl();
    EnterpriseService enterpriseService = new EnterpriseServiceImpl();
    public void ajaxlogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        /*System.out.println(account);*/
        Users user = new Users();
        user.setAccount(account);
        user.setPassword(password);
        boolean boo = userService.login(account,password);

        response.getWriter().println(boo);
    }


    public void ajaxFindUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        UserEnterprise user = userService.select(account,password);
        if(user == null){
            Users users = userService.findUser(account,password);
            Gson gson1 = new Gson();
            response.getWriter().println(gson1.toJson(users));
        }else{
            Gson gson = new Gson();
            response.getWriter().println(gson.toJson(user));
        }
    }


    public void ajaxadd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String idcard_name = request.getParameter("idcard_name");
        String idcard_no = request.getParameter("idcard_no");
        String enterprise_name = request.getParameter("enterprise_name");
        String social_uniform_code = request.getParameter("social_uniform_code");
        String scale = request.getParameter("scale");
        String email = request.getParameter("email");
        String ephone = request.getParameter("ephone");
        String fax = request.getParameter("fax");
        String address = request.getParameter("address");

        Boolean boo = true;
        if((account).equals("") || (password).equals("") || (idcard_name).equals("") || (idcard_no).equals("") || (enterprise_name).equals("") || (social_uniform_code).equals("") ||
                (scale).equals("") || (email).equals("") || (ephone).equals("") || (fax).equals("") || (address).equals("")){

            boo = false;
        }else{
            Boolean boo2 = enterpriseService.addEnterprise(enterprise_name,social_uniform_code,email,ephone,address,scale,fax);
            Enterprise enterprise = enterpriseService.selectAll(enterprise_name);
            int eid = enterprise.getEid();
            Boolean boo1 = userService.adduser(eid,enterprise_name,account,password,idcard_name,idcard_no);
            /*System.out.println(boo1);*/
            if(boo1 && boo2){
                boo = true;
            }else{
                boo = false;
            }
        }
        response.getWriter().println(boo);
    }

    public void judgeCode(HttpServletRequest request, HttpServletResponse response) throws IOException {

        /* 判断信用码是否重复
         *
         *
         * 查全部企业对象 获得所有的信用码
         *
         * */
        String social_uniform_code = request.getParameter("social_uniform_code");
        ArrayList<SocialCode> list = enterpriseService.findCode();
        Boolean boo3 = false;
        for (int i = 0; i < list.size(); i++) {
            SocialCode sc = list.get(i);
            /*System.out.println(sc.getSocial_uniform_code());*/
            if(social_uniform_code.equals(sc.getSocial_uniform_code())){
                /*System.out.println("重复了");*/
                boo3 = false;
                break;
            }else{
                /*System.out.println("没重复");*/
                boo3 = true;
            }
        }
        response.getWriter().println(boo3);
    }


    public void judeEname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enterprise_name = request.getParameter("enterprise_name");
        ArrayList<SocialCode> list = enterpriseService.findCode();
        Boolean boo4 = false;
        for (int i = 0; i < list.size(); i++) {
            SocialCode sc = list.get(i);
            if(enterprise_name.equals(sc.getEnamess())){

                /*System.out.println("名称重复了");*/
                boo4 = false;
                break;
            }else{
                /*System.out.println("名称没重复");*/
                boo4 = true;
            }
        }
        response.getWriter().println(boo4);

    }





    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        try {
            //1.获得客户端发送过来的请求地址
            String str = request.getRequestURI();
//        str.lastIndexOf("/");
            String methodname = str.substring(str.lastIndexOf("/") + 1);
            Class cla = UserController.class;
            Method declaredMethod = cla.getDeclaredMethod(methodname, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
